import 'dart:async';
import 'dart:math';
import 'package:flame/components.dart';
import 'package:freedom/page/mahjong/common/card_build.dart';
import 'package:freedom/page/mahjong/mahjong_game.dart';
import 'package:freedom/page/mahjong/util/user_location.dart';
import '../common/common_card.dart';
import '../common/mahjong_context.dart';
import 'abstract_component.dart';

/// *
/// 当前用户BOTTOM出牌区域
class TopExportArea extends AbstractExport with HasGameRef<MahjongIndexGame> {
  TopExportArea()
      : super(
          anchor: Anchor.center,
        );

  List<CommonCard> exportCardTempArray = [];

  @override
  FutureOr<void> onLoad() {
    size = MahjongContext().topExportAreaSize;
    position = MahjongContext().topExportAreaPoint;
    //debugMode = true;
    //添加测试出牌数据
    //testAddCard();
    super.onLoad();
    angle = radians(180);
  }

  @override
  Vector2 getLast() {
    // 屏幕的宽度减去 区域定位的中心宽度减去区域的一半加上最后一个元素的位移
    return Vector2(
        MahjongContext().screenWidth -
            position.x -
            size.x / 2 +
            getLastPoint().x,
        MahjongContext().screenHeight -
            position.y +
            size.y / 2 +
            MahjongContext().exportCardSize.y +
            getLastPoint().y);
  }

  /// 获取此compont 最后一张牌落地的位置

  @override
  Vector2 getLastPoint() {
    //一行向下取整
    var rowSum = (size.x / MahjongContext().topExportCardSize.x).floor();
    var lastPos = Vector2(
        (size.x - (rowSum - 2) * MahjongContext().topExportCardSize.x) / 2,
        MahjongContext().topExportCardSize.y);
    if (eportAllCard.length > 0) {
      var last = eportAllCard.last;
      lastPos = Vector2(last.position.x + last.size.x, lastPos.y);
      //计算出当前所在行和当前行最大数
      var addRowCount = 0;
      var currentRow = 0;
      while (eportAllCard.length + 1 > addRowCount) {
        currentRow++;
        addRowCount += ((currentRow - 1) * 2 + rowSum);
      }
      lastPos.y = MahjongContext().topExportCardSize.y * (currentRow - 1) +
          MahjongContext().topExportCardSize.y;
      //如果当前行最大数减去最后一行的数量就应该换行了
      if (addRowCount - rowSum - (currentRow - 1) * 2 == eportAllCard.length) {
        // 产生换行并且横向个数 +1
        // 数据在这里发生过反转所以跟bottom有反差
        CommonCard upFi = eportAllCard.toList()[0];
        //首个麻将要产生位移
        lastPos.x = upFi.position.x - upFi.size.x * (currentRow - 1);
      }
      // 第二排的时候向上位移高度的0.15 避免落差感
      if (currentRow > 1) {
        lastPos.y -=
            (MahjongContext().exportCardSize.y * 0.15) * (currentRow - 1);
      }
    }
    return lastPos;
  }

  /// 添加卡牌测试数据
  ///
  /// 涉及到翻转删除所以不能直接添加卡牌
  /// ！！！！！！！！！！！！！！！！！！！！！！！！！！
  /// 需要一个临时对象存储
  @override
  void reflashUI(List<int> cards) async {
    eportAllCard.clear();
    removeAll(Set.from(children));
    for (int i = 0, j = cards.length; i < j; i++) {
      var cardInfo = CardBuildUtil.topExport(getLastPoint(), cards[i]);
      eportAllCard.add(cardInfo
        ..priority = cardIndex--
        ..angle = pi);
    }
    for (var element in eportAllCard) {
      add(element);
    }
    var gameInfo = MahjongContext.roomInfo.gameInfo;
    if (eportAllCard.isNotEmpty &&
        gameInfo.exportUserId == currentUserState.userId &&
        eportAllCard.last.cardCode == gameInfo.exportCard) {
      eportAllCard.last.addMark();
    }
  }

  @override
  Direction userDirection() {
    return Direction.TOP;
  }

  @override
  CommonCard exportCard(int card) {
    var cardInfo = CardBuildUtil.topExport(getLastPoint(), card);
    eportAllCard.add(cardInfo
      ..priority = cardIndex--
      ..angle = pi);
    add(cardInfo);
    return cardInfo;
  }

  @override
  Vector2 getLastPointByScc(Vector2 currentCardPoint) {
    //获取出牌相当于屏幕的所在
    Vector2 targetScreenPoint =
        MahjongContext().topHandAreaPoint + currentCardPoint;
    //获取反转过后 相对于屏幕的右上角所在位置
    Vector2 exportPoint = Vector2(
        MahjongContext().topExportAreaPoint.x +
            MahjongContext().topExportAreaSize.x / 2,
        MahjongContext().topExportAreaPoint.y +
            MahjongContext().topExportAreaSize.y / 2);
    //获取资源位移距离
    Vector2 sourceScreenPoint = Vector2(
            exportPoint.x -
                getLastPoint().x -
                MahjongContext().topExportCardSize.x,
            exportPoint.y -
                getLastPoint().y -
                MahjongContext().topExportCardSize.y) +
        MahjongContext().topExportCardSize;
    Vector2 range = sourceScreenPoint - targetScreenPoint;
    return range;
  }
}
